#!/bin/sh
#################################################################################################
#												#
#	Vector Linux Package Builder Utility							#
# This scripts serves as  a backend to the vpackager utility. You should never have to		#
# invoke this script manually.									#
# Builds a Vector Linux Package (.tlz or .tgz) from a provided tarball (.tar.gz or .tar.bz2)	#
# Works pretty much like a slackbuild, only it's much more flexible 				#
# 												#
# CREDITS:											#
# Uelsk8s <Vector Linux>									#
# Easuter <Vector Linux>									#
# Blusk   <Vector Linux>									#
# Larhzu  <Tukaani linux>									#
# ^^^^^^^^^^^^^^^^^^^^^^^^^ Helped with ideas to write the code.				#
# Copyright (C)2007 Moises Henriquez <M0E.lnx@gmail.com>					#
# All rights reserved										#
#												#
#  This script is free software; you may redistribute it and/or modify				#
#  it under the terms of the GNU General Public License as published by				#
#  the Free Software Foundation. See http://www.gnu.org/licenses/gpl.txt			#
#  for full license details.									#
#												#
# All redistributions of this script must retain the above copyright notice.			#
#  												#
#  												#
#												#
#################################################################################################

CWD=`pwd`
TMP="/tmp"
MYNAME="vlpbulib"
MYVER="1.0RC1" # 06/18/07
DEFAULTCONFIG="--prefix=/usr \
--sysconfdir=/etc \
--bindir=/usr/bin \
--libdir=/usr/lib \
--localstatedir=/var \
--mandir=/usr/man"
### Allow a building profile with gettext support
STDCONFIG="--prefix=/usr \
--sysconfdir=/etc \
--bindir=/usr/bin \
--localstatedir=/var \
--mandir=/usr/man \
--with-included-gettext"

PKGDIR=${TMP}/${APP}-${APP_VER}
INSTALLDIR=${PKGDIR}/package-${APP}
PKG=${INSTALLDIR}

function build_the_stuff() {
#determine cflags
if [ ${USR_CFLAGS} = "default" ]; then
	# check for cflags based on architecture
		if [ ${PKG_ARCH} = "i386" ]; then
			SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
		elif [ ${PKG_ARCH} = "i486" ]; then
			SLKCFLAGS="-O2 -march=i486 -mtune=i686"
		elif [ ${PKG_ARCH} = "i586" ]; then
			SLKCFLAGS="-O2 -march=i586 -mtune=i686"
		elif [ ${PKG_ARCH} = "i686" ]; then
			SLKCFLAGS="-O2 -march=i586 -mtune=i686"
		fi
else
SLKCFLAGS=${USR_CFLAGS}
fi
# Set working env.
PKGDIR=${TMP}/${APP}-${APP_VER}
cd $PKGDIR
#if [ -e $PWD/configure ]; then
echo " "
echo " Building ${APP} with ${CONFIG} "
echo " CFLAGS = ${SLKCFLAGS}"
echo ""
sleep 2
#set -e
#CFLAGS = ${SLKCFLAGS}
./configure ${CONFIG} CFLAGS="${SLKCFLAGS}"
	if [ $? -gt 0 ]; then # Check for errors during config
	echo "Error: Configure failed!"
	exit 1
	fi
umask 0022 
make 
	if [ $? -gt 0 ]; then # Check for error during make
	echo "Error: Make failed!"
	exit 1
	else
	# Locate .desktop file after the make process
	    if [ -e $PWD/$APP.desktop ]; then
#		sleep 5
		echo " "
		echo " I've located a desktop file for $APP"
		mkdir -p $TMP/$APP-$APP_VER/package-$APP/usr/share/applications
		cp $APP.desktop $TMP/$APP-$APP_VER/package-$APP/usr/share/applications/
		sleep 5
		fi
	fi
#fi
echo "Running checkinstall"
#checkinstall -L -y --pkgarch=${PKG_ARCH} --pkgversion=${APP_VER} --pkgrelease=${PKG_REL}
mkdir -P package-${APP} && cd package-${APP}
make install DESTDIR=$PWD/package-${APP}
check4desc # Check for description
#makepkg ${APP}-${APP_VER}-${PKG_ARCH}-${PKG_REL}.tlz
#else
# echo "\
# 
#  Error: configure script not found... I dont know how to build this."
# exit 1
#fi
}


function extract() {
#	if [ ! -z ${SRCLOC} ] || [ -e ${SRCLOC} ]; then
# Try CD'ing into the tmp dir and then calling tar
if [ -e ${SRCLOC} ]; then
	if [ ! -d $TMP ]; then
		mkdir -p $TMP
	else
		cd $TMP
	fi
tar xfv ${SRCLOC} # extract the source
if [ $? -gt 0 ]; then
echo "\

An error has occurred in the untar process"

exit 1
else
#chmod 0700 /tmp/$APP-$APP_VER # Caused some problems to some users.
build_the_stuff
fi
else
echo "\

Error: No source specified 

" #should never happen
exit 1
fi
# 
# if [ -e ${SRCLOC} ]; then
# 	tar xfv ${SRCLOC} -C ${TMP} #-C /tmp
# 		if [ $? -gt 0 ]; then
# 		echo " An error has occurred in the untar process"
# 		exit
# 			else
# build_the_stuff		#build
# #go_for_it
# 			fi
# 
# 	else
# 	echo "Error: No source specified"
# 	exit 1
# 	fi

}

function check4desc() {
#if [ -z ${DESC_LOC} ]; then
if [ ${DESC_LOC} = "none" ]; then
sleep 1
echo " "
echo " No description file specified ... This package has no description"
echo ""
#Fix_Man
sleep 1
#echo "${APP}: ${APP}" > ${PKGDIR}/package-${APP}/install/slack-desc
fix_slack_desc
else
sleep 1
echo ""
echo " Relocating Description File"
echo ""
FINALDESCLOC=${PKGDIR}/package-${APP}/install
mkdir -p ${FINALDESCLOC} && cp ${DESC_LOC} ${FINALDESCLOC}
fix_slack_desc
fi
}
function go_for_it() {
PKGDIR=${TMP}/${APP}-${APP_VER}
PKG=${TMP}/${APP}-${APP_VER}/package-${APP}
cd $PKGDIR
echo ""
echo " Building ${APP} with ${CONFIG} "
echo ""
# Set configure options 
set -e
if [ -z ${CONFIG} ]; then
CONFIG=${DEFAULTCONFIG}
fi
./configure $CONFIG && umask 0022 && make && echo " Done with make"
mkdir -p $PKG # package-${APP}
#make DESTDIR=$PWD/package-${APP} install
#make install DESTDIR=$PWD/package-${APP}
make DESTDIR=$PKG install
echo " Done with make install"
set +e
check4desc
#Fix_Man
}
function package() {
cd ${TMP}/${APP}-${APP_VER}/package-${APP}
#sleep 1
echo "\

 Now packaging the stuff

"
#PKGNAME=${APP}-${APP_VER}_${PKG_ARCH}_${PKG_REL}.tlz # The tlz could be changed later
#FIXED_PACKAGENAME=${APP}-${APP_VER}-${PKG_ARCH}-${PKG_REL}.tlz # Fix the package naming bug
PKGNAME=${APP}-${APP_VER}_${PKG_ARCH}_${PKG_REL}.${PKG_TYPE} # The tlz could be changed later
FIXED_PACKAGENAME=${APP}-${APP_VER}-${PKG_ARCH}-${PKG_REL}.${PKG_TYPE} # Fix the package naming bug
cd ${TMP}/${APP}-${APP_VER}/package-${APP}
#makeslapt --tlz ${PKGNAME} && echo "\
#/sbin/makeslapt --${PKG_TYPE} ${PKGNAME} 
/sbin/makeslapt --$PKG_TYPE $FIXED_PACKAGENAME
if [ $? -gt 0 ]; then
exit 1
fi

echo  "\
		
${FIXED_PACKAGENAME} Created successfully

"
#mv ${PKGNAME} ${FIXED_PACKAGENAME}
}

function Fix_Man() {
echo ""
echo " Fixing man pages"
echo ""
PKG=${TMP}/${APP}-${APP_VER}/package-${APP}
cd $PKG
# Fine tune the package
find . | xargs file | grep "executable" |grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
sleep 4
#echo "Done stripping libraries."
# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
( cd $PKG/usr/man
	for manpagedir in $(find . -type d -name "man*") ; do
( cd $manpagedir
	for eachpage in $( find . -type l -maxdepth 1) ; do
ln -s $( readlink $eachpage ).gz $eachpage.gz
rm $eachpage
	done
	gzip -9 *.?
	)
done
)
fi
rm -rf $PKG/usr/share/doc
( cd $PKG
	find . -name perllocal.pod | xargs rm -f
)
mkdir -p ${PKG}/usr/doc/${APP}-${APP_VER}
cp /tmp/$APP-$APP_VER/AUTHORS \
/tmp/$APP-$APP_VER/COPYING \
/tmp/$APP-$APP_VER/INSTALL \
/tmp/$APP-$APP_VER/NEWS \
/tmp/$APP-$APP_VER/README \
/tmp/$APP-$APP_VER/TODO $PKG/usr/doc/$APP-$APP_VER/
#docs \
#${PKG}/usr/doc/${APP}-${APP_VER}
cp -r /tmp/$APP-$APP_VER/docs $PKG/usr/doc/$APP-$APP_VER/
rm -rf ${PKG}/usr/doc/${APP}-${APP_VER}/docs/Make*
rm -rf ${PKG}/usr/doc/${APP}-${AP_VER}/docs/*.1
rm -rf ${PKG}/usr/doc/${APP}-${AP_VER}/docs/help

( cd ${PKG}/usr/doc/${APP}-${APP_VER}/docs/
if [ ! -d help ] || [ ! -e help ]; then
ln -s ${PKG}/usr/share/${APP}/help 
fi
)
# ; ln -s ${PKG}/usr/share/${APP}/help . )
mkdir -p ${PKG}/install
#echo " Done fixing man pages"
#sleep 1
package
}
function fix_slack_desc() {
PKGDIR=${TMP}/${APP}-${APP_VER}/package-${APP}
FINALDESCLOC=${TMP}/${APP}-${APP_VER}/package-${APP}/install
if [ ${DESC_LOC} = "none" ]; then
mkdir -p $FINALDESCLOC
touch $FINALDESCLOC/slack-desc
echo ""
else
cd $PKGDIR
echo "\

 Appending slack-desc

"
# cat << EOF >> install/slack-desc
if [ -e ${FINALDESCLOC} ]; then
cat << EOF >> ${FINALDESCLOC}/slack-desc
#-------------------------------------------------------------------------------

BUILDDATE: `date`
PACKAGER : ${PKGR_ID}
HOST     : `uname -srm`
DISTRO   : `cat /etc/vector-version`
CFLAGS   : ${CFLAGS}
CONFIGURE: ${CONFIG}
EOF
#Fix_Man
else
echo "\
${APP}: ${APP}" > ${FINALDESCLOC}/slack-desc

at << EOF >> ${FINALDESCLOC}/slack-desc
#-------------------------------------------------------------------------------

BUILDDATE: `date`
PACKAGER : ${PKGR_ID}
HOST     : `uname -srm`
DISTRO   : `cat /etc/vector-version`
CFLAGS   : ${CFLAGS}
CONFIGURE: ${CONFIG}
EOF

fi
fi
#package
Fix_Man
}
function build() {
	#cd $PKGDIR
if [ ${THERE_IS_DESCFILE} = "1" ]; then # || [ -e ${DESC_LOC} ]; then
		cp ${DESC_LOC} ${PKGDIR}
echo ""
echo " Coyping description file"
echo ""
else
				echo "\
				
I can't find the description file you specified. Make sure the path is right

"
echo "\
			
 WARNING: This package will have no description
 
 "
fi
#	cd ${TMP}/${APP}-${APP_VER}
PKGDIR=${TMP}/${APP}-${APP_VER}
cd ${PKGDIR}
#cd ${TMP}/${APP}-${APP_VER}
#mkdir ${TMP}/${APP}-${APP_VER}/package-${APP}
#exit
if [ $? -gt 0 ]; then
	echo " An error occurred while trying to cd to the source dir."
else
	if [ -e configure ]; then
	echo "
			
 Running Configure ... Please Wait
			
	"
	set -e
		if [ -z ${CONFIG} ]; then
		CONFIG=${DEFAULTCONFIG}
		fi
		if [ ${THERE_IS_DESCFILE} ]; then
		cp ${DESC_LOC} $PWD
		echo ""
		echo "Using ${DESC_LOC} for package description"
		echo ""
		fi
	echo "Using These config options: " $CONFIG
sleep 1
			./configure $CONFIG
			if [ $? -gt 0 ]; then
			echo "\

ERROR: The configure process returned an error! 

"
	exit
			
	else
	 umask 0022 && make
			fi
		set +e
			
PKGNAME=${APP}-${APP_VER}_${PKG_ARCH}_${PKG_REL}.tlz # The tlz could be changed later
FIXED_PACKAGENAME=${APP}-${APP_VER}-${PKG_ARCH}-${PKG_REL}.tlz # Fix the package naming bug
		mkdir -p package-${APP}/install
# Fix the man pages
Fix_Man
			# copy description file if present
			#if [ ${THERE_IS_DESCFILE} = "1" ]; then
				if [ -e $PWD/slack-desc ]; then
				echo " Relocating description file ..."
				cp $PWD/slack-desc $PWD/package-${APP}/install
				fix_slack_desc
				else
				echo " Description file not present"
				fi
			#	fi
		make DESTDIR=${TMP}/${APP}-${APP_VER}/package-${APP} install && cd ${TMP}/${APP}-${APP_VER}/package-${APP}

#		make DESTDIR=${INSTALLDIR} install && cd ${ISNTALLDIR}
# NEED TO MODIFY TO ALLOW DIFFERENT PACKAGE TYPES.
#	makeslapt --tlz ${PKGNAME} && echo "\
makeslapt --tlz $FIXED_PACKAGENAME
		
#${PKGNAME} Fixing package name

#"
#mv ${PKGNAME} ${FIXED_PACKAGENAME}
echo "\
Done.
"
			#checkinstall -L -y --pkgarch=${PKG_ARCH} --pkgversion=${APP_VER} --pkgrelease=${PKG_REL}
			else
				echo "Error: No configure script found. I dont know how to build this yet."
				exit
			fi
	fi

}

# All flags should be passed for this to work...

#build
# Takes care of the flags passed out to the command called
while [ $# -gt 0 ]; do
	case "$1" in
		-s)
			SRCLOC="${2}"; # Assigns a source location
			shift;;
		-a)
			APP="${2}"; # Assign a name to the application (coming from the GUI"
			shift;;
		-v)
			APP_VER="${2}"; # Assign a version (mainly just t0 be able to cd to the source
			shift;;
		-d)
			DESC_LOC="${2}"; # Gives the path to a description file
			shift;;
		-c)
			CONFIG="${2}";  # Assign custom config options
			shift;;
		-pa)
			PKG_ARCH="${2}"; # package arch
			shift;;
		-pr)
			PKG_REL="${2}"; # Package release
			shift;;
		-pt)
			PKG_TYPE="${2}"; # package type
			shift;;
		-pkid)
			PKGR_ID="${2}"; # packager ID
			shift;;
		-cflags)
			USR_CFLAGS="${2}"; #Custom Cflags
			shift;;
		-t)
			TMP="${2}"; # set the tmp dir
			shift ;;
		-*)
			echo "You need to tell me where the source is...";
			exit;;
		*)
			break;;
	esac
shift
done

if [ "${CONFIG}" = "default" ]; then
	CONFIG=$DEFAULTCONFIG
elif [ "${CONFIG}" = "58std" ]; then
	CONFIG=$STDCONFIG
#else
#    $CONFIG = ${CONFIG}
fi
if [ ${DESC_LOC} = "none" ]; then
THERE_IS_DESCFILE="0"
else
THERE_IS_DESCFILE="1"
fi

echo " "
echo " Creating ${APP}-${APP_VER}-${PKG_ARCH}-${PKG_REL}.${PKG_TYPE}"
echo ""
sleep 2
extract
